const { db } = require('../config/database');

class Photo {
  // 创建照片记录
  static create(user_id, file_path, facial_features) {
    return new Promise((resolve, reject) => {
      const stmt = db.prepare('INSERT INTO photos (user_id, file_path, facial_features) VALUES (?, ?, ?)');
      stmt.run(user_id, file_path, facial_features, function(err) {
        if (err) {
          reject(err);
        } else {
          resolve({
            id: this.lastID,
            user_id,
            file_path,
            facial_features
          });
        }
      });
      stmt.finalize();
    });
  }

  // 根据ID查找照片
  static findById(id) {
    return new Promise((resolve, reject) => {
      db.get('SELECT * FROM photos WHERE id = ?', [id], (err, row) => {
        if (err) {
          reject(err);
        } else {
          resolve(row);
        }
      });
    });
  }

  // 根据用户ID查找照片
  static findByUserId(user_id) {
    return new Promise((resolve, reject) => {
      db.all('SELECT * FROM photos WHERE user_id = ?', [user_id], (err, rows) => {
        if (err) {
          reject(err);
        } else {
          resolve(rows);
        }
      });
    });
  }
}

module.exports = Photo;